1、@RestController

Spring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合。

即@RestController是@ResponseBody和@Controller的组合注解。

2、@RequestMapping 配置url映射

3、@PathVariable url参数化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.study.spring;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("hello")
public class helloDemo {

/**
* http://localhost:8083/spring-boot-study/hello/info?name=tom&sex=%E7%94%B7
* @param info
* @param request
* @return
*/
@RequestMapping("{info}")
public Map<String, String> info(@PathVariable String info, HttpServletRequest request) {
Map<String, String> map = new HashMap<>();

String name = request.getParameter("name");
String sex = request.getParameter("sex");

map.put("name", name);
map.put("sex", sex);
return map;
}

/**
* http://localhost:8081/spring-boot-study/hello/is/tom
* @param username
* @return
*/
@RequestMapping("is/{username}")
public String getUser(@PathVariable String username){
return "hello, " + username;
}
}

4、JPA相关注解

@GeneratedValue

@EntityListeners(AuditingEntityListener.class):监听JPA实体持久化

5、@Transient

在保存数据表的时候,忽略改字段,使其不 insert 该字段。

6、@JsonIgnore

import com.fasterxml.jackson.annotation.JsonIgnore;

返回实体时,该字段不返回,忽略。

7、@JsonProperty、@JSONField

import com.fasterxml.jackson.annotation.JsonProperty;

import com.alibaba.fastjson.annotation.JSONField;

@JsonProperty(“stat_time”)

用对象接收参数时,默认接收的字段是实体类中的属性字段,如果需要自定义接收的参数时,可以使用注解 @JsonProperty(“stat_time”) 。

@JSONField(name = “SourceNode”)

用对象展示参数时,默认展示的字段是实体类中的属性字段。如果需要自定义展示的参数时,如参数首字母大写,这时,可以使用 @JSONField(name = “SourceNode”)。

抽空研究下:spring.jackson.property-naming-strategy = SNAKE_CASE ??